package com.shenmazong.review.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.review.mapper.*;
import com.shenmazong.review.pojo.*;
import com.shenmazong.review.service.TbAskPaperDetailService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.IdVo;
import com.shenmazong.vo.ReplyQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 *
 */
@Service
public class TbAskPaperDetailServiceImpl extends ServiceImpl<TbAskPaperDetailMapper, TbAskPaperDetail>
    implements TbAskPaperDetailService{

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    TbQuestionMapper tbQuestionMapper;

    @Autowired
    TbAnswerMapper tbAnswerMapper;

    @Autowired
    TbUserReviewRecMapper tbUserReviewRecMapper;

    @Autowired
    TbUserReviewMapper tbUserReviewMapper;

    /**
     * @description 获取的问题从redis的list中获取
     * @author 军哥
     * @date 2022/4/7 9:36
     * @version 1.0
     */
    @Override
    public ResultResponse getReviewQuestion(IdVo idVo) {

        String key = "review_"+idVo.getId();
        Integer questionId = (Integer)redisTemplate.opsForList().rightPop(key);
        if(questionId == null) {

            // 更新评测状态为完成状态
            TbUserReview tbUserReview = tbUserReviewMapper.selectById(idVo.getId());
            tbUserReview.setReviewStatus(1);
            tbUserReviewMapper.updateById(tbUserReview);

            // 没有问题了，评测结束
            return ResultResponse.FAILED(600, "评测结束了");
        }

        // 发送评测问题
        TbQuestion tbQuestion = tbQuestionMapper.selectById(questionId);
        if(tbQuestion == null) {
            // 没有问题了，评测结束
            return ResultResponse.FAILED(-1, "评测问题不存在");
        }

        // 获取答案的ID
        QueryWrapper<TbAnswer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbAnswer::getQuestionId,questionId);
        queryWrapper.lambda().orderByAsc(TbAnswer::getAnswerId);
        List<TbAnswer> tbAnswers = tbAnswerMapper.selectList(queryWrapper);

        // 返回问题信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("question", tbQuestion);
        map.put("answers", tbAnswers);

        return ResultResponse.SUCCESS(map);
    }

    @Override
    public ResultResponse replyReviewQuestion(ReplyQuestionVo replyQuestionVo) {

        TbUserReviewRec tbUserReviewRec = new TbUserReviewRec();
        tbUserReviewRec.setUserId(replyQuestionVo.getUserId());
        tbUserReviewRec.setReviewId(replyQuestionVo.getReviewId());
        tbUserReviewRec.setAnswerId(replyQuestionVo.getAnswerId());
        tbUserReviewRec.setQuestionId(replyQuestionVo.getQuestionId());

        // 获取问卷ID
        TbUserReview tbUserReview = tbUserReviewMapper.selectById(replyQuestionVo.getReviewId());
        tbUserReviewRec.setPaperId(tbUserReview.getPaperId());

        // 存入用户的答案
        tbUserReviewRecMapper.insert(tbUserReviewRec);
        return ResultResponse.SUCCESS();
    }
}




